<template>
  <div class="consult-pay-page" v-if="payInfo.actualPayment">
    <nav-bar title="支付" />
    <div class="pay-info">
      <p class="tit">图文问诊 {{ payInfo.actualPayment }} 元</p>
      <img class="img" src="@/assets/images/avatar-doctor.svg" />
      <p class="desc"><span>极速问诊</span><span>自动分配医生</span></p>
    </div>
    <van-cell-group>
      <van-cell title="优惠券" :value="`-¥${payInfo.couponDeduction}`" />
      <van-cell title="积分抵扣" :value="`-¥${payInfo.pointDeduction}`" />
      <van-cell
        title="实付款"
        class="pay-price"
        :value="`-¥${payInfo.actualPayment - payInfo.couponDeduction}`"
      />
    </van-cell-group>
    <div class="pay-space"></div>
    <van-cell-group v-if="patientInfo.name">
      <van-cell
        title="患者信息"
        :value="
          `${patientInfo.name} | ${patientInfo.gender === 1 ? '男' : '女'} | ${
            patientInfo.age
          }岁`
        "
      />
      <van-cell title="病情描述" :label="consult.info.illnessDesc" />
    </van-cell-group>
    <div class="pay-schema">
      <van-checkbox v-model="isAgress"
        >我已同意 <span class="text">支付协议</span></van-checkbox
      >
    </div>
    <van-submit-bar
      :price="payInfo.actualPayment * 100"
      button-text="立即支付"
      button-type="primary"
      text-align="left"
      @submit="onSubmit"
      :disabled="!isAgress"
    />
    <!-- 支付面板 -->
    <pay-sheet
      v-if="orderId"
      v-model:show="show"
      :order-id="orderId"
      :actualPayment="payInfo.actualPayment"
      :onClose="closePay"
      :payCallback="payCallback"
    />
  </div>
</template>

<script lang="ts" setup>
import { Toast, Dialog } from 'vant'
import { ApiRes } from '@/types/data'
import { PayInfo } from '@/types/consult'
import { Patient } from '@/types/patient'
import request from '@/utils/request'
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import useStore from '@/store'
const router = useRouter()
const { consult } = useStore()
// 判断问诊信息是否完整
const judgeInfo = () => {
  if (!consult.info.patientId) {
    Toast({
      message: '问诊信息不完整，即将跳转到首页',
      duration: 1000,
      onClose: () => {
        router.push('/')
      }
    })

    return
  }
}
judgeInfo()
const payInfo = ref({} as PayInfo)
const patientInfo = ref({} as Patient)
const getPayInfo = async () => {
  const res = await request.get<ApiRes<PayInfo>>('patient/consult/order/pre', {
    params: {
      type: 2, // 极速问诊
      illnessType: consult.info.illnessType // 极速问诊的类型（0：普通、1：三甲）
    }
  })

  if (res.data.code === 10000) {
    payInfo.value = res.data.data
  }
}
const getPatientInfo = async () => {
  const res = await request.get<ApiRes<Patient>>(
    `patient/info/${consult.info.patientId}`
  )

  if (res.data.code === 10000) {
    patientInfo.value = res.data.data
  }
}
onMounted(() => {
  getPayInfo()
  getPatientInfo()
})

// 提交订单，弹出支付框组件
const isAgress = ref(true)
let orderId = ref('')
const show = ref(false)
const onSubmit = async () => {
  const res = await request.post<ApiRes<{ id: string }>>(
    'patient/consult/order',
    {
      ...consult.info,
      type: 2
    }
  )
  if (res.data.code === 10000) {
    consult.clear()
    orderId.value = res.data.data.id
    setTimeout(() => {
      show.value = true
    }, 0)
  }
}
const payCallback = '/room'
// 关闭支付
const closePay = () => {
  Dialog.confirm({
    title: '关闭支付',
    message: '取消支付将无法获得医生回复，医生接诊名额有限，是否确认关闭？',
    cancelButtonText: '任要关闭',
    confirmButtonText: '继续支付',
    confirmButtonColor: '#16c2a3'
  })
    .then(() => {
      // on confirm
    })
    .catch(() => {
      // on cancel
      orderId.value = ''
      show.value = false
      router.push(`/my/consult`)
    })
}
</script>

<style lang="scss" scoped>
.consult-pay-page {
  padding: 46px 0 50px 0;
}
.pay-info {
  display: flex;
  padding: 15px;
  flex-wrap: wrap;
  align-items: center;
  .tit {
    width: 100%;
    font-size: 16px;
    margin-bottom: 10px;
  }
  .img {
    margin-right: 10px;
    width: 38px;
    height: 38px;
    border-radius: 4px;
    overflow: hidden;
  }
  .desc {
    flex: 1;
    > span {
      display: block;
      color: var(--cp-tag);
      &:first-child {
        font-size: 16px;
        color: var(--cp-text2);
      }
    }
  }
}
.pay-price {
  ::v-deep() {
    .vam-cell__title {
      font-size: 16px;
    }
    .van-cell__value {
      font-size: 16px;
      color: var(--cp-price);
    }
  }
}
.pay-space {
  height: 12px;
  background-color: var(--cp-bg);
}
.pay-schema {
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  .text {
    color: var(--cp-primary);
  }
}
::v-deep() {
  .van-submit-bar__button {
    font-weight: normal;
    width: 160px;
  }
}
</style>
